/**
 * @description: 请求
 * 2022/3/23 15:31
 * @author: zsjin
 */
import axios, { AxiosRequestConfig } from 'axios';

const instance = axios.create({
  baseURL: import.meta.env.VITE_AXIOS_BASE_URL as string | undefined,
});

export async function restProxy<T = any>(config: AxiosRequestConfig): Promise<T> {
  return await instance.request(config);
}

instance.interceptors.response.use(response =>{
  if (response.config.method?.toLocaleLowerCase() === 'head') {
    return response.headers;
  }

  const resContentType = response.headers['content-type'];
  if (resContentType && resContentType.includes('text/plain') && typeof response.data === 'object') {
    return response.request.responseText;
  }

  return response.data;
});
